package com.google.code.securityflow.util

import java.lang.StringBuilder

class StopWatch() {

  var beginTime = 0;
  var events: List[(String, Int)] = List();

  def getTime: Int = (System.nanoTime() / 1000000L).toInt;

  def start() {
    beginTime = getTime;
  }

  def click(name: String) {
    events = (name, getTime - beginTime) :: events;
  }

  def toPrettyString: String = {
    val sb = new StringBuilder();
    var totalTime = 0;
    for ((name, time) <- events.reverse) {
      sb.append(name).append(": ").append(time).append("ms.\n");
      totalTime = totalTime + time;
    }
    sb.append("\nTotal Time: " + totalTime + "ms.");
    sb.toString;
  }

}